home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / amipop17.zip / POP_DOPO.C < prev    next >
C/C++ Source or Header  |  1993-12-08  |  7KB  |  449 lines

  1. /* Modified 11/30/93 by Tim Wadsworth according to Tomi Ollila's AmiTCP */
  2. /* diffs for AmiPOP v. 1.6.  Thanks Tomi!                               */
  3.  
  4. #include "pop.h"
  5.  
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/param.h>
  9. #include <errno.h>
  10. #include <netdb.h>
  11.  
  12. #ifdef AMITCP
  13. #define SockBase SocketBase
  14. #define cleanup_sockets()
  15. #define s_close(s) CloseSocket(s)
  16. #else
  17. #include <ss/socket.h>
  18. #endif
  19.  
  20. /* Variables global to this file */
  21.  
  22. struct Library *SockBase ;
  23. int havemail;
  24.  
  25. /* Functions */
  26.  
  27. int dopop(void)
  28. {
  29.     int s;
  30.     int count;
  31.     int okay=1;
  32.     struct hostent *hp;
  33.     struct sockaddr_in sa;
  34.  
  35.     sprintf(title,"Connecting to %s",pophost);
  36.     settitle();
  37.  
  38.     if(
  39. #ifdef AMITCP
  40.        (SocketBase = OpenLibrary( "bsdsocket.library", 2L ))
  41. #else    
  42.        (SockBase = OpenLibrary( "inet:libs/socket.library", 1L ))
  43. #endif
  44.        == NULL)
  45.     {
  46.         doreq("Error opening socket.library\n",bum);
  47.         return(1);
  48.     }
  49.  
  50. #ifdef AMITCP
  51.     SetErrnoPtr(&errno, sizeof errno);
  52. #else
  53.     setup_sockets( MAXSOCKS, &errno );
  54. #endif
  55.  
  56.     if((hp=gethostbyname(pophost))==NULL)
  57.     {
  58.         cleanup_sockets();
  59.         CloseLibrary( SockBase ) ;
  60.         doreq("Connection Refused.",bum);
  61.         return(1);
  62.     }
  63.  
  64.     bzero(&sa, sizeof(sa));
  65.     bcopy(hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
  66.     sa.sin_family = hp->h_addrtype;
  67.     sa.sin_port = htons((u_short)port);
  68.     if ((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0)
  69.     {
  70.         cleanup_sockets();
  71.         CloseLibrary( SockBase ) ;
  72.         doreq("Something bad\nhas happened.",bum);
  73.         return(1);
  74.     }
  75.  
  76.     if (connect(s,(struct sockaddr *) &sa,sizeof(sa))< 0)
  77.     {
  78.         doreq("No POP3 daemon\nrunning on this\nmachine or port.",bum);
  79.         s_close(s);
  80.         cleanup_sockets();
  81.         CloseLibrary( SockBase ) ;
  82.         return(1);
  83.     }
  84.  
  85. /* Put actual code here */
  86.  
  87.     recv(s,buf,BUFSIZE-1,0);
  88.     sprintf(title,"Got Connection");
  89.     settitle();
  90.  
  91.     sendgreet(s);
  92.  
  93.     if (senduser(s))
  94.     {
  95.         cleanup_sockets();
  96.         CloseLibrary( SockBase ) ;
  97.         return(1);
  98.     }
  99.  
  100.     count=havemail=sendstat(s);
  101.  
  102.     while (count && okay)
  103.     {
  104.         sprintf(title,"Retrieving %i",count);
  105.         settitle();
  106.  
  107.         okay=retrieve(s,count);
  108.  
  109.         if (delmail)
  110.         {
  111.             sprintf(title,"Deleting %i",count);
  112.             settitle();
  113.  
  114.             okay=delmessage(s,count);
  115.         }
  116.         --count;
  117.     }
  118.  
  119.     if(sendquit(s))
  120.     {
  121.         cleanup_sockets();
  122.         CloseLibrary( SockBase ) ;
  123.         return(1);
  124.     }
  125.  
  126.     if (Project0Wnd)
  127.     {
  128.         SetWindowTitles(Project0Wnd,Project0Wdt, (UBYTE *) ~0);
  129.     }
  130.  
  131.     if (havemail && notify) /* removed sendstat() after Nofify */
  132.     {
  133.         doreq("You have new mail.","Cool");
  134.     }
  135.  
  136. /* End actual code */
  137.  
  138.     s_close(s);
  139.     cleanup_sockets();
  140.     CloseLibrary( SockBase ) ;
  141.     return(0);
  142. }
  143.  
  144. int sendgreet(int s)
  145. {
  146.     sprintf(buf,"HELLO\r\n");
  147.     trans(s);
  148.  
  149.     return(0);
  150. }
  151.  
  152. int senduser(int s)
  153. {
  154.     int t;
  155.  
  156.     sprintf(title,"Sending Username");
  157.     settitle();
  158.  
  159.     sprintf(buf,"USER %s\r\n",username);
  160.  
  161.     if ( !trans(s) ) return(1);
  162.  
  163.     t=sscanf(buf,"%s",temp);
  164.     if ( valcheck(t,temp) ) return(1);
  165.  
  166. /* Password */
  167.  
  168.     sprintf(title,"Sending Password");
  169.     settitle();
  170.  
  171.     sprintf(buf,"PASS %s\r\n",password);
  172.  
  173.     if ( !trans(s) ) return(1);
  174.  
  175.     t=sscanf(buf,"%s",temp);
  176.  
  177.     if ( valcheck(t,temp) ) return(1);
  178.  
  179.     return(0);
  180. }
  181.  
  182. int sendquit(int s)
  183. {
  184.     int t;
  185.  
  186.     sprintf(title,"Sending QUIT");
  187.     settitle();
  188.  
  189.     sprintf(buf,"QUIT\r\n");
  190.  
  191.     if ( !trans(s) ) return(1);
  192.  
  193.     t=sscanf(buf,"%s",temp);
  194.  
  195.     if ( valcheck(t,temp) ) return(1);
  196.  
  197.     sprintf(title,"Quit Acknowledged");
  198.     settitle();
  199.  
  200.     return(0);
  201. }
  202.  
  203. int sendstat( int s )
  204. {
  205.     int t;
  206.     int count=0;
  207.  
  208.     sprintf(buf,"STAT\r\n");
  209.  
  210.     if ( !trans(s) ) return(0);
  211.  
  212.     t=sscanf(buf,"%s %i",temp,&count);
  213.  
  214.     if ( valcheck(t,temp) ) return(0);
  215.  
  216.     return(count);
  217. }
  218.  
  219. int retrieve(int s,int count)
  220. {
  221.     int foo;
  222.     int c;
  223.     int goon=1;
  224.  
  225.     char *havefrom;
  226.     BOOL begin=TRUE;
  227.     BPTR ofp;
  228.  
  229.     if (havefrom=AllocVec(512,MEMF_CLEAR))
  230.     {
  231.         if ( (count == havemail) && (!appfile) )
  232.         {
  233.             ofp = Open(maildir, MODE_NEWFILE );
  234.         }
  235.         else
  236.         {
  237.             ofp = Open(maildir, MODE_READWRITE);
  238.         }
  239.  
  240.         if ( ofp == 0)
  241.         {
  242.             doreq("Open() failed\n",bum);
  243.             FreeVec(havefrom);
  244.             return(0);
  245.         }
  246.  
  247.         Seek(ofp,0 ,OFFSET_END);
  248.  
  249.         sprintf(buf,"RETR %i\r\n",count);
  250.  
  251.         if ( !trans(s) )
  252.         {
  253.             Close(ofp);
  254.             FreeVec(havefrom);
  255.             return(0);
  256.         }
  257.  
  258.         if ( valcheck(1,buf) )
  259.         {
  260.             Close(ofp);
  261.             FreeVec(havefrom);
  262.             return(0);
  263.         }
  264.  
  265.         if (strstr(buf,"\r\n"))
  266.         {
  267.             FPuts(ofp,strstr(buf,"\r\n")+2);
  268.         }
  269.  
  270.         while ( goon )
  271.         {
  272.             foo=recv(s,buf,BUFSIZE-1,0);
  273.             buf[foo]='\0';
  274.  
  275.             goon=lastblock(buf);
  276.  
  277.             if (foo < 1)
  278.             {
  279.                 goon=0;
  280.             }
  281.  
  282.             if (begin) /* Magic to add From header */
  283.             {
  284.                 if (buf[0] != 'F')
  285.                 {
  286.                     struct DateTime dt;
  287.                     char day[20]="";
  288.                     char date[20]="";
  289.                     char time[20]="";
  290.                     char *newtemp;
  291.  
  292.                     if (newtemp=AllocVec(2*BUFSIZE,MEMF_CLEAR))
  293.                     {
  294.                         DateStamp(&dt.dat_Stamp);
  295.  
  296.                         dt.dat_Format = FORMAT_USA;
  297.                         dt.dat_Flags = 0;
  298.                         dt.dat_StrDay = day;
  299.                         dt.dat_StrDate = date;
  300.                         dt.dat_StrTime = time;
  301.  
  302.                         DateToStr(&dt);
  303.  
  304.                         sprintf(newtemp,"From %s@%s %s %s %s\n", username,pophost, day, date, time);
  305.                         FPuts(ofp,newtemp);
  306.                         FreeVec(newtemp);
  307.                     }
  308.                 }
  309.             }
  310.             begin=FALSE;
  311.  
  312.             strip();
  313.  
  314.             c=FPuts(ofp,buf);
  315.  
  316.             if (c != 0)
  317.             {
  318.                 doreq("FPuts() failed!",bum);
  319.                 goon=0;
  320.             }
  321.         }
  322.  
  323.         FPuts(ofp,"\n");
  324.         Close(ofp);
  325.  
  326.         FreeVec(havefrom);
  327.         return(1);
  328.     }
  329.     else
  330.     {
  331.         return(0);
  332.     }
  333.  
  334. }
  335.  
  336. int delmessage( int s, int count )
  337. {
  338.     sprintf(buf,"DELE %i\r\n",count);
  339.     trans(s);
  340.  
  341.     if ( valcheck(1,buf) ) return(0);
  342.  
  343.     return(1);
  344. }
  345.  
  346. int valcheck(int t, char *localtemp)
  347. {
  348.     if (t == 0)
  349.     {
  350.         doreq("scanf() failed\n",bum);
  351.         return(1);
  352.     }
  353.  
  354.     if ( !strstr(localtemp,"+OK") )
  355.     {
  356.         doreq("Didn't get +OK",bum);
  357.  
  358.         if (localtemp[0] != '\0')
  359.         {
  360.             doreq(localtemp,bum);
  361.         }
  362.         return(1);
  363.     }
  364.     return(0);
  365. }
  366.  
  367. int trans( int s )
  368. {
  369.     ULONG foo;
  370.  
  371.     send(s,buf,strlen(buf),0);
  372.  
  373.     foo=recv(s,buf,BUFSIZE-1,0);
  374.     buf[foo]='\0';
  375.  
  376.     if ( !foo )
  377.     {
  378.         return(0);
  379.     }
  380.  
  381.     return (1);
  382. }
  383.  
  384. void settitle( void )
  385. {
  386.     if (winop)
  387.     {
  388.         SetWindowTitles(Project0Wnd,title, (UBYTE *) ~0);
  389.     }
  390. }
  391.  
  392. void strip( void )
  393. {
  394.     char out[BUFSIZE]="";
  395.     ULONG x1=0;
  396.     ULONG x2=0;
  397.     ULONG len;
  398.  
  399.     len=strlen(buf);
  400.  
  401.     while (buf[x1] != '\0' )
  402.     {
  403.         while (buf[x1] == '\r')
  404.         {
  405.             ++x1;
  406.         }
  407.         out[x2]=buf[x1];
  408.         ++x2;
  409.         if (x1 < len)
  410.         {
  411.             ++x1;
  412.         }
  413.     }
  414.     out[x2]='\0';
  415.  
  416.     strcpy(buf,out);
  417. }
  418.  
  419. int lastblock (char *segment)
  420. {
  421.     char *found;
  422.     int len = strlen(segment);
  423.  
  424.     if ( (found=strstr(segment,"\r\n.\r\n")))
  425.     {
  426.         buf[(found-segment)-1]='\n';
  427.         buf[found-segment]='\0'; /* Kill '.' at end of message */
  428.         return(0);
  429.     }
  430.  
  431.     if ( (len==2) && (found=strstr(segment,"\r\n")) )
  432.     {
  433.         return(0);
  434.     }
  435.  
  436.     if ( len==1 ) /* Kludge of the century */
  437.     {
  438.         return(0);
  439.     }
  440.  
  441.     if ( (len <= 4) && (found=strstr(segment,".\r\n")) )
  442.     {
  443.         buf[found-segment]='\0'; /* Kill '.' at end of message */
  444.         return(0);
  445.     }
  446.  
  447.     return(1);
  448. }
  449.